package cn.hs.ph.serviet;

import cn.hs.ph.dao.CommunityDao;
import cn.hs.ph.dao.impl.CommunityDaoimpl;
import cn.hs.ph.entity.Community;
import cn.hs.ph.util.JDBCDruidUtil;
import cn.hs.ph.util.Result;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/community")
public class CommunityServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        CommunityDaoimpl communityDaoimpl = new CommunityDaoimpl();
        if ("selectAll".equals(action)) {
            String pageNum = req.getParameter("pageNum");
            String pageSize = req.getParameter("pageSize");
            String startTime = req.getParameter("startTime");
            String endTime = req.getParameter("endTime");
            String communityName = req.getParameter("communityName");
            Map<String,String> map = new HashMap<>();
            map.put("pageNum",pageNum);
            map.put("pageSize",pageSize);
            map.put("startTime",startTime);
            map.put("endTime",endTime);
            map.put("communityName",communityName);


            List<Community> list = null;
            try {
                list = communityDaoimpl.selectAll(map);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            int total = 0;
            try {
                total = communityDaoimpl.selectCount(map);
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
            HashMap hashMap = new HashMap();
            hashMap.put("total",total);
            hashMap.put("list",list);

            Result success = Result.success(hashMap);
            resp.getWriter().write(JSON.toJSONString(success));


        }else if("update".equals(action)){
            String addOrUpdateFlag = req.getParameter("addOrUpdateFlag");
            String communityAddress = req.getParameter("communityAddress");
            String code = req.getParameter("communityAreaCode");
            int communityAreaCode=0;
            if (code!=null && !code.equals("")){
                communityAreaCode=Integer.parseInt(code);
            }
            String communityCity = req.getParameter("communityCity");
            String communityCoordinates = req.getParameter("communityCoordinates");
            String communityDistrict = req.getParameter("communityDistrict");
            String id = req.getParameter("communityId");
            int communityId=0;
            if (id!=null&&!id.equals("")){
                communityId = Integer.parseInt(id);
            }
            String communityLatitude = req.getParameter("communityLatitude");
            String communityLongitude = req.getParameter("communityLongitude");
            String communityName = req.getParameter("communityName");
            String communityProvince = req.getParameter("communityProvince");
Community community = new Community(communityId,  communityName,  communityProvince,  communityCity,  communityDistrict,  communityAddress,  communityLatitude,  communityLongitude,  communityAreaCode,  communityCoordinates);
      if ("1".equals(addOrUpdateFlag)){
          int insert = communityDaoimpl.insert(community);
          Result result=null;
          if (insert!=0){
             result= Result.success("新增成功");

          }else {
             result = Result.error("新增失败");
          }
          resp.getWriter().write(JSON.toJSONString(result));
      }else {
          int insert = communityDaoimpl.update(community);
          Result result=null;
          if (insert!=0){
              result= Result.success("修改成功");

          }else {
              result = Result.error("修改失败");
          }
          resp.getWriter().write(JSON.toJSONString(result));


      }

        }else if (action.equals("delete")){
            String id = req.getParameter("communityId");
             int communityId=Integer.parseInt(id);
            int delete = communityDaoimpl.delete(communityId);
            Result result=null;
            if (delete!=0){
                result=Result.success("删除成功");
            }else {
                result=Result.error("删除失败");
            }
            resp.getWriter().write(JSON.toJSONString(result));


        }


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);

    }
}
